//
//  ZPropertyController.m
//  NewProject
//
//  Created by Apple on 2018/5/17.
//  Copyright © 2018年 chenIos. All rights reserved.
//

#import "ZPropertyController.h"
#import "ZWPropertyHeadView.h"
#import "ZWPropertyCell.h"
#import "ZWAddNewPropertyController.h"
#import "ZWPropertyDetailController.h"
#import "ZWalletInfo.h"
//#import "ZFilterController.h"
@interface ZPropertyController ()<UITableViewDelegate,UITableViewDataSource,AddPropertyProtocol>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) ZWPropertyHeadView *headView;
@property (nonatomic,strong) ZWalletAccountInfo *selectedAccount;
//@property (nonatomic,strong) NSNumber *pais;
@property(nonatomic,strong)ZWalletInfo *walletInfo;
@end

@implementation ZPropertyController
-(void)viewWillAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    [self requestCoinNumberData];
//    ZWalletAccount *wallet = [ZWalletAccount wallet];
//    [self.tableView reloadData];
//    [_headView configWithName:self.selectedAccount.name withAddress:self.selectedAccount.walletAdress currencyUnit:wallet.currentCurrrencyUnit money:self.selectedAccount.pais];

}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.tableView.mj_header endRefreshing];
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
   
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = COLOR_WHITE;
    [self.view addSubview:self.tableView];
    _headView = [[ZWPropertyHeadView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_W*0.73)];
    _headView.backgroundColor = COLOR_RED;
    _headView.delegate = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestCoinNumberData)];
    self.tableView.tableHeaderView = _headView;

}

-(void)requestCoinNumberData{
    ZWWeakSelf;
    [ZDBManager qureySelectedWallet:^(NSDictionary *walletInfo) {
        [weakSelf.tableView.mj_header endRefreshing];
        weakSelf.selectedAccount = [ZWalletAccountInfo mj_objectWithKeyValues:walletInfo];
        NSString *url = [NSString stringWithFormat:@"%@wallet/getAssetList",PortServer];
        NSDictionary *param  = @{@"address":weakSelf.selectedAccount.walletAdress};
        ZWLog(@"url = %@\n param = %@",url,param);
        [ZNetManager GET:url parameters:param success:^(NSURLSessionDataTask *task, id  _Nullable responseObject) {
            NSInteger status = [responseObject[@"status"] integerValue];
            if(status == 1){
                ZWalletInfo *info = [ZWalletInfo mj_objectWithKeyValues:responseObject[@"data"]];
                [weakSelf.headView configWithName:weakSelf.selectedAccount.name withAddress:weakSelf.selectedAccount.walletAdress currencyUnit:@"" money:info.total.stringValue];
                weakSelf.walletInfo = info;
                [weakSelf.tableView reloadData];
            }else
            {
                [ZProgressManager showError:responseObject[@"message"]];
            }
            ZWLog(@"responseObject = %@",responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError *error) {
            [ZProgressManager showError:@"连接失败"];
             ZWLog(@"error = %@",error.userInfo);
        }];

        
       
    }];
}
#pragma mark --添加新的资产--

-(void)addNewPropertyMethod{
    ZWAddNewPropertyController *newControl = [[ZWAddNewPropertyController alloc]init];
    [self.navigationController pushViewController:newControl animated:YES];
}
#pragma mark - tableView datasource and delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.walletInfo ? 1 : 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.walletInfo.assetList.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return Scare_Width(82);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZWPropertyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZWPropertyCell" forIndexPath:indexPath];
    ZWalletInfoList *item = self.walletInfo.assetList[indexPath.row];
    [cell setCurrencyUnit:[ZWalletAccount wallet].currentCurrrencyUnit pais:item.balance currency:item.currency money:item.cny];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = COLOR_WHITE;
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ZWPropertyDetailController *detailControl = [[ZWPropertyDetailController alloc]init];
    ZWalletInfoList *item = self.walletInfo.assetList[indexPath.row];
    detailControl.currency = item.currency;
    
    detailControl.walletAccount = self.selectedAccount;
    [self.navigationController pushViewController:detailControl animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc]init];
}
#pragma mark ---set----
- (UITableView *)tableView {
    if (!_tableView) {
        CGFloat nav = iPhoneX ? 44 : 20;
        CGFloat bottom = iPhoneX ? 34 + 49 : 49;
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -nav, SCREEN_W, SCREEN_H - bottom ) style:0];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = COLOR_WHITE;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZWPropertyCell class] forCellReuseIdentifier:@"ZWPropertyCell"];
    }
    return _tableView;
}

@end
